package org.micode.libs.dao.base;

import lombok.extern.slf4j.Slf4j;
import org.micode.common.domain.Page;
import org.micode.common.domain.base.BaseEntity;
import org.micode.common.domain.base.BaseQuery;

import java.util.List;
import java.util.Map;

/**
 * 服务类,基本方法
 */
@Slf4j
public abstract class BaseQueryServiceImpl
        <KT, DTO extends BaseEntity<KT>, DO extends BaseEntity<KT>,
                QY extends BaseQuery<DO>, DAO extends BaseQueryDao<DTO>>
        extends BaseInitServiceImpl<KT, DTO, DO, QY, DAO>
        implements BaseQueryService<KT, DTO, QY> {

    public BaseQueryServiceImpl() {
        super();
    }

    @Override
    public long countByExample(DTO example) {
        List<Long> ret = getMyDao().countByExample(example);

        if (null != ret && ret.size() > 0 && null != ret.get(0)) {
            return ret.get(0);
        }
        return 0;
    }

    @Override
    public List<DTO> listByExample(DTO example, Page page) {
        return listByExample(example, page, null);
    }

    @Override
    public List<DTO> listByExample(DTO example, Page page, Map<String, String> orderMap) {
        String orderBy = getOrderByString(orderMap);
        return getMyDao().listByExample(example, page, orderBy);
    }

    @Override
    public DTO getOne(DTO example) {
        List<DTO> entities = listByExample(example, Page.oneRecordPage());
        if (null != entities && entities.size() > 0) {
            return entities.get(0);
        }
        return null;
    }

}
